package Servlet.Teacher;

import Entity.MyTableData;
import Service.Teacher.Imp.ReplyGroupServiceImp;
import Service.Teacher.ReplyGroupService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/9/4
 * Time: 15:41
 */

@WebServlet(name = "ReplyServlet", urlPatterns = {"/getAllGroup", "/allotGroup", "/addTimeAndAddress", "/allotStu"})
public class ReplyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getAllGroup".equals(url)) {
            getAllGroup(request, response);
        } else if ("/allotGroup".equals(url)) {
            allotGroup(request, response);
        } else if ("/addTimeAndAddress".equals(url)) {
            addTimeAndAddress(request, response);
        } else if ("/allotStu".equals(url)) {
            allotStu(request, response);
        }

    }

    private void allotStu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReplyGroupService replyGroupService = new ReplyGroupServiceImp();
        Integer code = replyGroupService.allotStudent();
        PrintWriter out = response.getWriter();
        out.print(code);
        out.close();
    }

    private void addTimeAndAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReplyGroupService replyGroupService = new ReplyGroupServiceImp();
        String replyTime = request.getParameter("replyTime");
        String replyAddress = request.getParameter("replyAddress");
        Integer id = Integer.parseInt(request.getParameter("id"));
        Boolean flag = replyGroupService.addTimeAndAddress(id, replyTime, replyAddress);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    private void allotGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReplyGroupService replyGroupService = new ReplyGroupServiceImp();
        String type = request.getParameter("type");
        Integer code = replyGroupService.allotGroup(type);
        PrintWriter out = response.getWriter();
        out.print(code);
        out.close();
    }

    private void getAllGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ReplyGroupService replyGroupService = new ReplyGroupServiceImp();
        MyTableData myTableData = replyGroupService.getAllGroup();
        PrintWriter out = response.getWriter();
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        out.print(json);
        out.close();
    }


}
